<?

define('MESSAGE_ERROR',   1);
define('MESSAGE_WARNING', 2);
define('MESSAGE_INFO',    3);

class Config
{

    var $config_root = '';

    function Config($root = '') {
        $GLOBALS['__config_root__'] = $root;
    }

    function getLink($path, $object='', $additional_params='') {
        $res = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'].'?path='.$path.($object != '' ? '&object='.$object : '');
        if ($additional_params != '') {
            if (substr($additional_params,0,1) == '&') {
                $additional_params = substr($additional_params,1);
            }
            if (substr($additional_params,-1) == '&') {
                $additional_params = substr($additional_params,0,-1);
            }
            $res .= '&'.$additional_params;
        }
        if (isset($_GET['params']) && ($_GET['params'] != '')) {
            $res .= '&params='.urlencode($_GET['params']);
        }
        return $res;
    }

    function getMsgLink($path, $object='', $message_type='', $message_text = '') {
        $res = Config::getLink($path,$object).($message_type != '' ? Config::getMessage($message_type,$message_text) : '');
        return $res;
    }

    function getParam($name) {
        if (!isset($_GET['params'])) {
            return '';
        }
        $rawparams = explode('&',urldecode($_GET['params']));
        foreach ($rawparams as $param) {
            $temp = explode('=',$param);
            $params[$temp[0]] = $temp[1];
        }
        if (isset($params[$name])) {
            return $params[$name];
        } else {
            return '';
        }
    }

    function getValue($config,$name) {
        $param = Config::getParam('mode');
        if (isset($GLOBALS['__config_root__'])) {
            $root = $GLOBALS['__config_root__'];
        } else {
            $root = '';
        }
        if ($param != '') {
            if (file_exists('etc/'.$param.'/'.$config.'.conf.php')) {
                require $root.'etc/'.$param.'/'.$config.'.conf.php';
            } else {
                require $root.'etc/'.$config.'.conf.php';
            }
        } else {
            require $root.'etc/'.$config.'.conf.php';
        }
        if ($name != '') {
            return $$name;
        } else {
            return '';
        }
    }

    function getMessage($message_type, $message_text) {
        switch ($message_type) {
            case MESSAGE_ERROR:   $name = 'error'; break;
            case MESSAGE_WARNING: $name = 'warning'; break;
            case MESSAGE_INFO:    $name = 'info'; break;
            default: return '';
        }
        return '&'.$name.'='.urlencode($message_text);
    }

}

?>